<template>
	<view class="nas-test">
		<view class="nas-test-password">
			<nas-input
				ref="input1"
				inputValue="13262740694"
				:verification="['isNull', 'isPhoneNum']"
				:verificationTip="['手机号码不能为空', '请输入正确的手机号']"
				placeholder="请输入手机号码"
				maBtm="10"
				maxLength="11"
			></nas-input>
		</view>
		<view class="nas-test-rule">*此页面为模拟智家云盘的入口，可参考该页面的逻辑接入</view>
		<button class="nas-test-button" @click="onerification">进入云NAS</button>
	</view>
</template>

<script>
import request from '../utils/request.js';
import nasAes from '../utils/nasAes.js';
import nasInput from '../components/nas-input/nas-input';
export default {
	data() {
		return {};
	},
	components: {
		nasInput
	},
	methods: {
		async onerification() {
			if (this.$refs.input1.getValue()) {
				uni.setStorageSync('nasPhone', this.$refs.input1.getValue())
				// 1、用户状态是未开通未注册，则跳转到没有资格提示页面
				// 2、用户状态是已开通未注册，则跳转到首次登录设置密码页面，设置完密码之后，获取token然后跳转到云NAS首页
				// 3、用户状态是已开通已注册，则先获取token，然后跳转到云NAS首页

				// 第一步、获取该手机号用户的当前状态：0:未开通未注册 1:已开通未注册 2:未开通已注册 3:已开通已注册 4:冻结状态
				const statusParams = {
					method: 'POST',
					url: '/nas/user/status',
					data: {
						mobile: this.$refs.input1.getValue()
					}
				};
				const resStatus = await request(statusParams);
				const status = resStatus.data;
				// 已开通已注册状态
				if (status == 3) {
					// 获取加密需要的key
					const keyParams = {
						method: 'POST',
						url: '/nas/session/key',
						data: {
							mobile: this.$refs.input1.getValue()
						}
					};
					const keyRes = await request(keyParams);
					const key = keyRes.data;
					// 获取智家云盘的token，对智家云盘的token进行加密
					const loginToken =
						uni.getStorageSync('token') ||
						'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZU51bWJlciI6IjEzMjYyNzQwNjk0IiwidXNlcklkIjo1ODgyNCwiaWF0IjoxNjU2MzE5OTIyMTY2LCJsb2dpblNvdXJjZSI6Il9hcHBfbG9naW5fc291cmNlXyJ9.rk03fvRA3D2rw07trUg0Xaz-Y7Y_x0wJcHq68yYNFgg';
					
					const tokenParams = {
						method: 'POST',
						url: '/naslocal/sdk/apptoken',
						data: {
							mobile: this.$refs.input1.getValue(),
							loginToken: nasAes.setAES(loginToken, key)
						}
					};
					console.log(tokenParams, 'tokenParams');
					const resToken = await request(tokenParams);
					if (resToken.data.access_token) {
						uni.setStorageSync('nasToken', resToken.data.access_token);
						uni.navigateTo({
							url: '/pages/nas/nas-home/nas-home'
						});
					} else {
						uni.showToast({
							icon: 'error',
							title: '获取token失败'
						});
					}
				} else if(status == 0){
					uni.navigateTo({
						url: '/pages/nas/nas-unregistered/nas-unregistered'
						// url: '/pages/nas/nas-login/nas-login'
					})
				} else if(status == 1) {
					uni.navigateTo({
						url: '/pages/nas/nas-login/nas-login'
					})
				}
				// uni.showToast({
				// 	icon: 'none',
				// 	title: '校验通过',
				// 	// #ifdef MP-WEIXIN
				// 	duration: 2000
				// 	// #endif
				// 	// mask: true
				// });
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.nas-test {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	padding-top: 108rpx;
	background: #fff;
	overflow: hidden;
	.nas-test-img {
		text-align: center;
		image {
			width: 176rpx;
			height: 108rpx;
		}
	}
	.nas-test-title {
		margin-top: 44rpx;
		text-align: center;
		image {
			width: 296rpx;
			height: 48rpx;
		}
	}
	.nas-test-password {
		margin: 44rpx 95rpx;
	}
	.nas-test-button {
		margin: 120rpx 95rpx 0 95rpx;
		height: 80rpx;
		background: #3069dc;
		border-radius: 40rpx;
		color: #fff;
		font-family: HarmonyOS_Sans_SC;
		font-size: 32rpx;
		color: #ffffff;
		text-align: center;
		font-weight: 400;
	}
	.nas-test-rule {
		text-align: center;
		margin: 21rpx 134rpx;
		font-family: HarmonyOS_Sans_SC;
		font-size: 24rpx;
		color: red;
		text-align: justify;
		font-weight: 400;
	}
}
</style>
